{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "'ascii' codec can't decode byte 0xe2 in position 42694: ordinal not in range(128)\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0mTraceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-06cf91a4d5a0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     18\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m             \u001b[1;32mprint\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m             \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import urllib2\n",
    "import time\n",
    "infile = open('/home/guan/Data/ceo_education/ceo_pagelist_30w')\n",
    "for n, line in enumerate(infile):\n",
    "    if n % 1000 == 0:\n",
    "        print n\n",
    "    line = line.strip().split(',')\n",
    "    ceo_name = line[0].strip()\n",
    "    ceo_link = line[1].strip()\n",
    "    outfile2 = open('/home/guan/Data/ceo_education/ceo_pages/30w/' + ceo_name + '.html', 'w')\n",
    "    #print ceo_name\n",
    "    crawl_success = 0\n",
    "    while crawl_success == 0:\n",
    "        try:\n",
    "            response = urllib2.urlopen(ceo_link)\n",
    "            print >> outfile2, response.read()\n",
    "            crawl_success = 1\n",
    "        except Exception as e:\n",
    "            print str(e)\n",
    "            time.sleep(10)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Al Majali.html\n",
      "Abdullah Al Majali\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Al Baz.html\n",
      "Abdullah Al Baz\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah M Habadi.html\n",
      "Abdullah M Habadi\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Bukhari.html\n",
      "Abdullah Bukhari\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Yaprakci.html\n",
      "Abdullah Yaprakci\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Ali Al Turaiki.html\n",
      "Abdullah Ali Al Turaiki\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Hamdan Al Zorqi.html\n",
      "Abdullah Hamdan Al Zorqi\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Yigitbasi.html\n",
      "Abdullah Yigitbasi\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Can.html\n",
      "Abdullah Can\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Cortu.html\n",
      "Abdullah Cortu\n",
      "/home/guan/Data/ceo_education/ceo_pages/test/Abdullah Abdul Rahman Al Kanhal.html\n",
      "Abdullah Abdul Rahman Al Kanhal\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "existing_files = glob.glob('/home/guan/Data/ceo_education/ceo_pages/test/*.html')\n",
    "existing_ceo = set()\n",
    "for existing_file in existing_files:\n",
    "    print existing_file\n",
    "    existing_file = existing_file.split('test/')[1].split('.')[0]\n",
    "    print existing_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
